package com.example.seh.person.buy.errand;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import com.example.seh.R;
import com.example.seh.chat.ChatActivity;
import com.example.seh.constant.Constant;
import com.example.seh.home.substitution.DeliverDTO;
import com.example.seh.person.sell.errand.ErrandAdapter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MyErrandsAdapter extends BaseAdapter {
    private Context context;
    private int item;
    private List<DeliverDTO> lists = new ArrayList<>();

    public MyErrandsAdapter(Context context, int item, List<DeliverDTO> lists) {
        this.context = context;
        this.item = item;
        this.lists = lists;
    }

    @Override
    public int getCount() {
        if (lists != null) {
            return lists.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (lists != null) {
            return lists.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      ViewHolder viewHolder;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(item, null);
            viewHolder = new ViewHolder();
            viewHolder.decs = convertView.findViewById(R.id.my_errand_decs);
            viewHolder.goodsAddress = convertView.findViewById(R.id.my_errand_goodsAddress);
            viewHolder.myAddress = convertView.findViewById(R.id.my_errand_myAddress);
            viewHolder.errandState = convertView.findViewById(R.id.sell_errand_state);
            viewHolder.substitutes = convertView.findViewById(R.id.callSubstitute);
            viewHolder.code = convertView.findViewById(R.id.my_errand_code);
            viewHolder.confirm = convertView.findViewById(R.id.confirm_errand);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.decs.setText(lists.get(position).getDeliverDesc());
        viewHolder.goodsAddress.setText(lists.get(position).getAddressName());
        viewHolder.myAddress.setText(lists.get(position).getReceiveName());
        viewHolder.code.setText(lists.get(position).getDeliverCode());


        if (lists.get(position).getDeliverStatus() == 1){
            viewHolder.errandState.setText("代取中");
        }else if (lists.get(position).getDeliverStatus() == 2){
            viewHolder.errandState.setText("代取完成");
        }

        // 设置 confirm 视图的可见性
        if (lists.get(position).getDeliverStatus() == 2) {
            viewHolder.confirm.setVisibility(View.GONE);
        } else {
            viewHolder.confirm.setVisibility(View.VISIBLE);
        }

        viewHolder.substitutes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int buyerId = lists.get(position).getUserId();
                Log.e("bought",lists.get(position).toString());
                Intent intent = new Intent(context, ChatActivity.class);
                intent.putExtra("user", lists.get(position).getUserDTO());
                context.startActivity(intent);
            }
        });

        viewHolder.confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder dialog = new AlertDialog.Builder(parent.getContext());
                dialog.setTitle("提示")
                        .setMessage("确定收到货了吗？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                viewHolder.errandState.setText("代取完成");
                                //发送后台修改代取状态
                                sendState(lists.get(position).getDeliverId(),viewHolder.confirm);
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });
                dialog.create().show();
            }
        });

        return convertView;
    }

    private void sendState(Integer deliverId,TextView confirm) {
        OkHttpClient okHttpClient = new OkHttpClient();

        Request request = new Request.Builder()
                .url(Constant.CONFIRM_DELIVER+deliverId)
                .get()
                .build();
        Log.e("url",request.url().toString());
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("okhttp", "发送数据失败");
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    Log.e("okhttp", "发送成功");
                    ((Activity) context).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            confirm.setVisibility(View.GONE);
                        }
                    });

                } finally {
                    // 关闭响应体
                    if (response != null && response.body() != null) {
                        response.body().close();
                    }
                }
            }
        });

    }

    class ViewHolder{
        TextView decs,goodsAddress,myAddress,substitutes,errandState,code,confirm;
    }

}
